package com.jindi.jacococli.api.aop.handler;

import com.dianping.cat.Cat;
import com.jindi.jacococli.biz.exception.BusinessException;
import com.jindi.jacococli.biz.exception.ParamException;
import com.jindidata.jindi.utils.common.ResultWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(value = ParamException.class)
    public ResultWrapper<?> paramExceptionHandler(ParamException e) {
        log.warn("[GlobalExceptionHandler][paramExceptionHandler]", e);
        return ResultWrapper.builder().warn(e.getMessage()).build();
    }

    @ResponseBody
    @ExceptionHandler(value = BusinessException.class)
    public ResultWrapper<?> businessExceptionHandler(BusinessException e) {
        log.warn("[GlobalExceptionHandler][businessExceptionHandler]", e);
        return ResultWrapper.builder().warn(e.getMessage()).build();
    }

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public ResultWrapper<?> exceptionHandler(Exception e) {
        log.error("[GlobalExceptionHandler][exceptionHandler]", e);
        Cat.logError(e);
        return ResultWrapper.builder().warn(e.getMessage()).build();
    }
}